package org.joshsacks.fishies;

import java.util.Random;

import org.joshsacks.fishies.GameBoard.GamePieceType;
import org.joshsacks.fishies.ui.gl.GameBoardRenderer;

public class GamePiece {
  public static int PIECE_SIZE = 3;
  private static Random random = new Random(System.currentTimeMillis());
  private GameBoard.GamePieceType[] pieces;
  private double gamePieceX = 4 * GameBoardRenderer.getTileSize();
  private double gamePieceY = GameBoard.HEIGHT * GameBoardRenderer.getTileSize();

  public static GamePiece generatePiece(int maxType) {
    GameBoard.GamePieceType[] pieces = new GameBoard.GamePieceType[PIECE_SIZE];
    for (int i = 0; i < PIECE_SIZE; i++) {
      pieces[i] = GamePieceType.values()[random.nextInt(maxType)];
    }
    
    return new GamePiece(pieces);
  }
  
  public void rotate() {
    GamePieceType type = pieces[0];
    pieces[0] = pieces[1];
    pieces[1] = pieces[2];
    pieces[2] = type;
  }
  
  private GamePiece(GamePieceType[] pieces) {
    this.pieces = pieces;
  }

  public double getX() {
    return gamePieceX;
  }

  public void setX(double gamePieceX) {
    this.gamePieceX = gamePieceX;
  }

  public double getY() {
    return gamePieceY;
  }

  public void setY(double gamePieceY) {
    this.gamePieceY = gamePieceY;
  }  
  
  public GamePieceType typeAtPosition(int position) {
    return pieces[position];
  }

  public int getColumn() {
    return (int) (gamePieceX / GameBoardRenderer.getTileSize() + 0.5) ;
  }

  public int getBottomRow() {
   return (int) (gamePieceY / GameBoardRenderer.getTileSize() - PIECE_SIZE);
  }
}
